import 'package:flutter/material.dart';
import 'dart:async';

import '../widget/jdText.dart';
import '../widget/jdButton.dart';
import 'package:dio/dio.dart';
import 'package:fluttertoast/fluttertoast.dart';
import '../config/config.dart';
class Register2Page extends StatefulWidget {
  final Map arguments;
  Register2Page({Key key,this.arguments}) : super(key: key);

  _Register2PageState createState() => _Register2PageState();
}

class _Register2PageState extends State<Register2Page> {
  String _tel;
  String _code;
  String _textValue;
  String sendButText = '重新发送';
  int _seconds = 5; 
  @override
  void initState() {
    super.initState();
    this._tel = widget.arguments['tel'];
    this._code = widget.arguments['code'];
    this._startTimer();
  }
 
  sendCode() async{
    var api = "${Config.domain}api/sendCode";
    var reslut = await Dio().post(api,data: {"tel":this._tel});
    if(reslut.data['success']){ 
       this._startTimer();
       print(reslut.data);
       setState(() {
        this._code = reslut.data['code']; 
       });
      Fluttertoast.showToast(
        msg: "${reslut.data['message']}",
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.CENTER
      );
    }
  }
  next(){
    if(this._code == this._textValue){
      Navigator.pushNamed(context, '/register3',arguments: {
        "code":this._code,
        "tel":this._tel
      });
    }else{
      Fluttertoast.showToast(
        msg: "验证码有误",
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.CENTER
      );
    }
  }
  void _startTimer() {
    Timer _timer;
     _timer = Timer.periodic(Duration(milliseconds:1000),(timer) {
         if (this._seconds == 0) {
            _timer.cancel();
            setState(() {
              this.sendButText ='重新发送';
              this._seconds =  10;
            });
            return;
         }
         setState(() {
            this._seconds--;
            this.sendButText ='${this._seconds}s';
         });
     });
  }
 
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("注册 - 2"),
      ),
      body: ListView(
        padding: EdgeInsets.fromLTRB(20, 0, 20, 0),
        children: <Widget>[ 
          SizedBox(height: 20,),
          Container( 
            padding: EdgeInsets.only(left: 10),
            child:Text("验证码已将发送到您的${_tel}手机，请输入验证码") ,
          ),
          SizedBox(height: 20,),
          Stack(
            children: <Widget>[
              JdText(
                text: '请输入验证码',
                onChanged: (value){
                  setState(() {
                   this._textValue = value; 
                  });
                },
              ),
              Positioned(
                right: 0,
                top: 0,
                child:RaisedButton(
                  child: Text(this.sendButText),
                  onPressed: (){
                    if(this._seconds == 10){
                      this.sendCode();
                    }
                  },
                ) ,
              )
            ],
          ),
          SizedBox(height: 20,),
          JdButtoon(
            color: Colors.red,
            text: '下一步',
            onTap: this.next,
          )
        ],
      ),
    );
  }
}